<?php
	session_start();
	
	//Check if file is called from ajax or is loaded from proper URL
	if (isset($_GET['self']) && $_GET['self'] == 'true' && $_GET['profile']){
		include('../inc/fnc.php');
		include('../inc/conn.php');
		$id = $_GET['profile'];
	} /* else {
		//include('inc/fnc.php');
		$id = $_GET['id'];
	} */
	
	// Getting Username from userId
	$id = getUserIdFromUsername($pageType);
	$name =  getUsername($id);
	$email =  getEmail($id);

	$avatarPath=userimage($id, 'large')."?time=".time();
	
	$query = mysql_query("SELECT DISTINCT a.storyid as sid, a.storyname as stitle, a.crtdtime as time FROM story a WHERE crtdbyid = {$id} and delflag = 'N' ORDER BY time");
	$specificstry = mysql_num_rows($query)/STORYINPAGE;
	
	$query = mysql_query("SELECT distinct(temp.storyid) as sid, Max(temp.time_post_vote) as ltime FROM
	(
		SELECT Distinct a.storyid, Max(a.crtdtime) as time_post_vote
		FROM entrylinevote a WHERE crtdbyid = {$id} and delflag = 'N' GROUP BY a.storyid

		UNION
		SELECT Distinct b.storyid, Max(b.crtdtime) as time_post_vote
		FROM story b WHERE crtdbyid = {$id} and delflag = 'N' GROUP BY b.storyid

		UNION
		SELECT Distinct c.storyid, Max(c.crtdtime) as time_post_vote
		FROM entryline c WHERE crtdbyid = {$id} and delflag = 'N' GROUP BY c.storyid
	) as temp
	GROUP BY temp.storyid ORDER BY ltime");
	$allstry = mysql_num_rows($query)/STORYINPAGE;


?>

<div id="content" class="">
    <div class="topShadow"></div>
    <div id="profile">
        <!--  Profile Starts -->
        <div id="avatar" class="left"><img id="dPic" alt="" src="<?php echo $avatarPath; ?>" style="width: 150px; height: 150px;"/></div>
        
        <!-- Detail Info -->
        <div id="sumup" class="left">
            <div class="display">
                <div id="namePlay">
                    <h1 class="left"><?php echo $name; ?></h1>
                    <div class="right">
                    <?php if($_SESSION['userId'] != $id && is_logged_in()){ ?>
							<button data-id="<?php echo $id; ?>" type="button" value="<?php echo $id; ?>" class="btnLb msg-new">+Message</button>
					<?php
						$temp = isFriend($id, $_SESSION['userId']);
						if($temp == "true"){ 
					?>
                        	<button id="dConnect" type="button" data-frenid="<?php echo $id; ?>"  data-myid='<?php echo $_SESSION['userId']; ?>' class="btnGrey">Unfollow</button> 
				  <?php } else { ?>
							<button id="connect" type="button" value="<?php echo $id; ?>" class="btnB">Follow</button> 
				  <?php } 
				   } ?>
                    </div>
                </div>
             </div>  <!-- ENDS  <div id="display"> -->
        </div>
        <div class="clear"></div>
        <!-- Detail Info Ends-->
        
        
        <ul id="msMenu">
            <li id="msAll" class="msNav <?php if(is_logged_in()){ echo 'proClick'; } ?> msCurrent"><a href="#all-stories" class="popLink">All Stories</a></li>
            <li id="msOwn" class="msNav <?php if(is_logged_in()){ echo 'proClick'; } ?>"><a href="#my-stories" class="popLink">My Stories</a></li>
            
            <li id="msFwing" class="msNav <?php if(is_logged_in()){ echo 'proClick'; } ?>"><a href="#all-follow" class="popLink">Following</a></li>
            <li id="msFwer" class="msNav <?php if(is_logged_in()){ echo 'proClick'; } ?>"><a href="#my-followers" class="popLink">Follower</a></li>
            
        </ul>
        <div id="msDisplay">
            <div id="msAllWrap" class="msWrap">
                <h1>All Stories</h1>
                <?php if(!is_logged_in()){ ?>
                	<span class="proMsg">You need to log in to view this content</span>
                <?php } else {
				if($_SESSION['userId'] == 1){
					echo "..";
				}


						if(isset($_POST['page'])) {
							$pag = $_POST['page'];
							$id = $_POST['id'];
							include('inc/fnc.php');
							include('inc/constants.php');
						} else {
							$pag = 1;
						}
						
						echo userallstories($id,$pag);
					  } ?>
                <div class="clear"></div>  
            </div>
            <div id="msMyWrap" class="msWrap hide">
                <h1>My  Stories</h1>
                <?php if(!is_logged_in()){ ?>
                	<span class="proMsg">You need to log in to view this content</span>
                <?php } else {
						if(isset($_POST['page'])){
							$pag = $_POST['page'];
							$id = $_POST['id'];
							include('inc/fnc.php');
							include('inc/constants.php');
						} else {
							$pag = 1;
						}
							$return = userspecificstories($id,$pag);
							echo $return;
					  } ?>
            </div>
            <div id="msFwingWrap" class="msWrap hide">
                <h1>Following</h1>
                <?php if(!is_logged_in()){ ?>
                    <span class="proMsg">You need to log in to view this content</span>
                <?php } else {
                        $query = "SELECT * FROM followme, users WHERE users.userid = followme.followingid AND followme.followerid = {$id} AND followme.delflag = 'N' AND `delete flag` = 'N' ORDER BY users.username";
						$result = mysql_query($query,$connection);
						$return = "<ul>";
						$i = 0;
						while($row = mysql_fetch_array($result)) {
							$path = userimage($row['followingid'],'small');
							$username = $row['username'];
							
							$return .= "<li class='fList' style='width: 80px; float: left; margin-right: 10px; text-align: center;' >
											<img src='{$path}' style='width: 40px; margin: 0 auto; text-align: center; margin-bottom: 5px;'>
											<div><a href='index.php?page=" . strtolower(getUsername($row['followingid'])) . "'>$username</a></div>
										</li>";
							$i++;
						}
						$return .= "</ul>";
						
						if ($i == 0 ){ $markup = "<ul><li>You are not following anyone at the moment.</li></ul>"; } else { $markup = $return; }
					
						echo $markup;
                    } ?>
                    <div class="clear"></div>
            </div>
            <div id="msFwerWrap" class="msWrap hide">
                <h1>My  Followers</h1>
                <?php if(!is_logged_in()){ ?>
                    <span class="proMsg">You need to log in to view this content</span>
                <?php } else {
                        $query = "SELECT * FROM followme, users WHERE users.userid = followme.followerid AND followme.followingid = {$id} AND followme.delflag = 'N' AND `delete flag` = 'N' ORDER BY users.username";
						$result = mysql_query($query,$connection);
						$return = "<ul>";
						$i = 0;
						while($row = mysql_fetch_array($result)) {
							$path = userimage($row['followerid'],'small');
							$username = $row['username'];
							
							$return .= "<li class='fList' style='width: 80px; float: left; margin-right: 10px; text-align: center;' >
											<img src='{$path}' style='width: 40px; margin: 0 auto; text-align: center; margin-bottom: 5px;'>
											<div><a href='index.php?page=" . strtolower(getUsername($row['followerid'])) . "'>$username</a></div>
										</li>";
							$i++;
						}
						if ($i == 0 ){ $markup = "<ul><li>No one is following you at the moment.</li></ul>"; } else { $markup = $return; }
					
						echo $markup;
                    } ?>
                    <div class="clear"></div>
            </div>
            <div class="nextPrev" style="text-align: center; margin-left: -85px;">
                <input id="upro" type="hidden" value="<?php echo $id;?>" />
                <input id="pagetp1" type="hidden" value="all" />
                <input id="pagenumber1" type="hidden" value="1" />
               <input id="maxspecific" type="hidden" value="<?php echo $specificstry; ?>"/>
	<input id="maxallpro" type="hidden" value="<?php echo $allstry; ?>"/>
            <input id="previousbtn" class="btnGrey" name="" type="button" value="Previous"/>
            <input id="nextbtn" <?php if($allstry > 1){ ?> class="btnB" <?php }else{ ?>class="btnGrey" <?php } ?> name="" type="button" value="Next"/>
            </div>
        </div>
        
        

    <div class="clear"></div>        
    </div> <!--  <div class="profile"> END -->
    <div class="bottomShadow"></div>
</div>